/**
 * while实现二分查找
 * @param array 源数组
 * @param target 目标值
 * @returns 目标值索引
 */
function binarySearch(array: Array<number>, target: number): number {
    let leftIndex = 0;
    let rightIndex = array.length - 1;
    let result = -1;
    while (leftIndex <= rightIndex) {
        console.log(`索引范围：${leftIndex + "---" + rightIndex}`);
        console.warn(`目标范围：${rightIndex - leftIndex}`);
        const midIndex = Math.floor((leftIndex + rightIndex) / 2);
        if (array[midIndex] === target) {
            return midIndex;
        } else if (array[midIndex] > target) {
            rightIndex = midIndex - 1;
        } else {
            leftIndex = midIndex + 1;
        }
    }
    return result;
}
function demo() {
    //@ts-ignore
    const mockArray = Array.from(new Array(Math.pow(2, 22)), (item, index) => {
        return index + 1;
    });
    binarySearch(mockArray, 500);
}
export default {
    name: "二分查找",
    core: binarySearch,
    event: demo,
};
